/**
 * 
 */
package org.gkolpu.validation.oval.basetypes;

import java.io.Serializable;

import net.sf.oval.configuration.annotation.IsInvariant;
import net.sf.oval.constraint.AssertTrue;
import net.sf.oval.constraint.Length;
import net.sf.oval.constraint.MatchPattern;
import net.sf.oval.constraint.NotMemberOf;

import org.gkolpu.validation.oval.basetypes.enums.PhoneType;

public class Phone implements Serializable {

	@Length(errorCode = "phone.number.length", min = 10, max = 10, profiles = { "MYPROFILE" })
	@MatchPattern(errorCode = "phone.number.digits.matchPattern", pattern = "^\\d+$", profiles = { "MYPROFILEY" })
	private String number;

	@NotMemberOf(errorCode = "phone.type.invalid", value = { "INVALID" }, profiles = { "MYPROFILE" })
	private PhoneType type;

	@AssertTrue(errorCode = "phone.type.set.number.notNull", profiles = { "MYPROFILE" })
	@IsInvariant
	public boolean validatePhoneNumberWhenTypeisSet() {
		boolean isValid = true;

		if (this.type != null && this.number == null) {
			isValid = false;
		}

		return isValid;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public PhoneType getType() {
		return type;
	}

	public void setType(PhoneType type) {
		this.type = type;
	}

	public Phone(String number, PhoneType type) {
		super();
		this.number = number;
		this.type = type;
	}

	@Override
	public String toString() {
		return "Phone [number=" + number + ", type=" + type + "]";
	}

}
